var token = localStorage.getItem("token"),
    schoolYearArr = ['2019-2020学年第1学期', '2019-2020学年第2学期', '2020-2021学年第1学期', '2020-2021学年第2学期',
        '2021-2022学年第1学期', '2021-2022学年第2学期', '2022-2023学年第1学期', '2022-2023学年第2学期'
    ],
    schoolYearNum = ['1-1', '1-2', '2-1', '2-2', '3-1', '3-2', '4-1', '4-2',],
    monthNum = ['一月份', '二月份', '三月份', '四月份', '五月份', '六月份', '七月份', '八月份', '九月份', '十月份', '十一月份', '十二月份'],
    defaultNum = 1,
    num = 0,
    dormitoryGrade = Sendajax("http://119.23.46.241:8080/login/dorPer", "post", {time: schoolYearNum[num] + '-' + defaultNum}, token);


setTimeout(function () {
    $(".school-year-text p").text(schoolYearArr[num])
    $(".school-month p").text(monthNum[defaultNum - 1])
    exam(dormitoryGrade)
}, 100)

localStorage.setItem('isCheckDormitoryResults', 1)
if (localStorage.getItem('isViewFinalGrade')) {
    $(".back").on("click", function () {
        window.history.back(-2)
    })
}


function exam(data) {
    let examData = ''
    if (data.code == 502) {
        examData += `<section class="no-content">
        <section></section>
         <section>
         <p>成绩表未上传或错误</p>
         <p>请联系管理员上传或修改</p>
         </section>
         </section>`
    }

    if (data.code == 201) {
        if (data.data == 0) {
            examData += `<section class="no-content">
        <section></section>
         <p>暂无内容</p>
         </section>`
        } else if (data.data.score.length == 0) {
            examData += `<section class="grade" id="only">
            <section class="fraction">
                <p>共计</p>
                <p>${data.data.zongfen}</p>
            </section>
            <section class="dormitory">
                <p class="dormitory-time">受罚学号（扣分最多）</p>
                <p class="dormitory-num">${data.data.pm.map((val) => {
                return '  ' + val
            }).join()}</p>
            </section>
        </section>`
            examData += `<section class="end">
            <section></section>
            <p>到底了</p>
        </section>`
        } else {
            $.each(data.data.score, function (i, val) {
                examData += `<section class="grade">
            <section class="fraction">
                <p>成绩</p>
                <p>${val.score}</p>
            </section>
            <section class="dormitory">
                <p class="dormitory-time">${val.time}</p>
                <p class="dormitory-num">${val.dormitory}</p>
            </section>
        </section>`
            })
            examData += `<section class="grade" id="last">
            <section class="fraction">
                <p>共计</p>
                <p>${data.data.zongfen}</p>
            </section>
            <section class="dormitory">
                <p class="dormitory-time">受罚学号（扣分最多）</p>
                <p class="dormitory-num">${data.data.pm.map((val) => {
                return '  ' + val
            }).join()}</p>
            </section>
        </section>`
            examData += `<section class="end">
            <section></section>
            <p>到底了</p>
        </section>`
        }
    }
    $(".grade-content").html(examData)
}

//下一学期
$(".next-school-year").on("click", function () {
    num++
    if (num >= 8) {
        num = 0
        $(".school-year-text p").text(schoolYearArr[num])
        $(".school-month p").text(monthNum[defaultNum - 1])
    } else {
        $(".school-year-text p").text(schoolYearArr[num])
        $(".school-month p").text(monthNum[defaultNum - 1])
    }
    dormitoryGrade = Sendajax("http://119.23.46.241:8080/login/dorPer", "post", {time: schoolYearNum[num] + '-' + defaultNum}, token);
    exam(dormitoryGrade)
})

//上一学期
$(".last-school-year").on("click", function () {
    num--
    if (num < 0) {
        num = 7
        $(".school-year-text p").text(schoolYearArr[num])
        $(".school-month p").text(monthNum[defaultNum - 1])
    } else {
        $(".school-year-text p").text(schoolYearArr[num])
        $(".school-month p").text(monthNum[defaultNum - 1])
    }
    dormitoryGrade = Sendajax("http://119.23.46.241:8080/login/dorPer", "post", {time: schoolYearNum[num] + '-' + defaultNum}, token);
    exam(dormitoryGrade)
})

//下一个月
$(".next-month").on("click", function () {
    defaultNum++
    if (defaultNum >= 12) {
        defaultNum = 0
        $(".school-year-text p").text(schoolYearArr[num])
        $(".school-month p").text(monthNum[defaultNum - 1])
    } else {
        $(".school-year-text p").text(schoolYearArr[num])
        $(".school-month p").text(monthNum[defaultNum - 1])
    }
    dormitoryGrade = Sendajax("http://119.23.46.241:8080/login/dorPer", "post", {time: schoolYearNum[num] + '-' + defaultNum}, token);
    exam(dormitoryGrade)
})

//上一个月
$(".last-month").on("click", function () {
    defaultNum--
    if (defaultNum <= 0) {
        defaultNum = 12
        $(".school-year-text p").text(schoolYearArr[num])
        $(".school-month p").text(monthNum[defaultNum - 1])
    } else {
        $(".school-year-text p").text(schoolYearArr[num])
        $(".school-month p").text(monthNum[defaultNum - 1])
    }
    dormitoryGrade = Sendajax("http://119.23.46.241:8080/login/dorPer", "post", {time: schoolYearNum[num] + '-' + defaultNum}, token);
    exam(dormitoryGrade)
})

$(".search-box input").keydown(function (e) {
    if (e.keyCode == 13) {
        let searchData = Sendajax("http://119.23.46.241:8080/login/finddp", "post", {
            time: schoolYearNum[num] + '-' + defaultNum,
            input: $(this).val()
        }, token)
        $(".search-box input").blur()
        exam(searchData)
    }
})